package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.config.LoginThreadLocal;
import com.project.entity.PromotionEntity;
import com.project.mapper.IPromotionMapper;
import com.project.service.IPromotionService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Service
@Transactional
public class PromotionServiceImpl implements IPromotionService {

    @Autowired
    private IPromotionMapper promotionMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public IPage<PromotionEntity> findByItem(int pageNO, int status, int pageSize) {
        QueryWrapper<PromotionEntity> qw = new QueryWrapper<>();
        qw.eq("p_status",status);
        return promotionMapper.selectPage(new Page<>(pageNO,pageSize),qw);
    }

    @Override
    public void updateStatus(Integer id, int status, String cause) {
        PromotionEntity promotionEntity = promotionMapper.selectById(id);
        promotionEntity.setStatus(status);
        promotionEntity.setVerifyName(LoginThreadLocal.getLoginVerify().getName());
        if (status != 1){
            promotionEntity.setCause(cause);
        }
        promotionMapper.updateById(promotionEntity);

        //Java对象转化为JSON对象
        String jsonObject =  JSONObject.toJSONString(promotionEntity);
//TODO: implement 未测试
        rabbitTemplate.convertAndSend("topicExchangeTwo","rabbit.PromotionVerifyResult",jsonObject);

    }

    @Override
    public PromotionEntity findById(Integer id) {
        return promotionMapper.selectById(id);
    }



    
}
